package com.xf.common.core.domain;


import com.fasterxml.jackson.annotation.JsonIgnore;
import com.xf.common.core.enums.StatusCodeEnum;


import java.io.Serializable;

/**
 * 普通返回
 **/

public class DmpResult<Resp> implements Serializable {
    private static final long serialVersionUID = 771836922595187944L;

    //返回数据
    protected Resp retData;
    //返回错误码
    protected String retCode;
    //返回消息

    protected String retMsg;

    public DmpResult() {
    }

    public DmpResult(Resp resp, String retCode, String retMsg) {
        this.retData = resp;
        this.retCode = retCode;
        this.retMsg = retMsg;
    }

    /**
     * 成功返回的默认方法
     *
     * @param <Resp>
     * @return
     */
    public static <Resp> DmpResult success() {
        return new DmpResult(null, StatusCodeEnum.SUC.getCode(), null);
    }

    public static <Resp> DmpResult success(String msg) {
        return new DmpResult(null, StatusCodeEnum.SUC.getCode(), msg);
    }

    /**
     * 数据返回的默认方法
     *
     * @param resp
     * @param <Resp>
     * @return
     */
    public static <Resp> DmpResult data(Resp resp) {
        return new DmpResult(resp, StatusCodeEnum.SUC.getCode(), null);
    }

    public static <Resp> DmpResult data(Resp resp, String msg) {
        return new DmpResult(resp, StatusCodeEnum.SUC.getCode(), msg);
    }

    /**
     * 错误的默认方法
     *
     * @param <Resp>
     * @return
     */
    public static <Resp> DmpResult failed() {
        return new DmpResult(null, StatusCodeEnum.ERROR.getCode(),null);
    }

    public static <Resp> DmpResult failed(String msg) {
        return new DmpResult(null, StatusCodeEnum.ERROR.getCode(), msg);
    }

    public static DmpResult failed(StatusCodeEnum statusCodeEnum) {
        return new DmpResult(null, statusCodeEnum.getCode(),null);
    }

    public static DmpResult failed(StatusCodeEnum statusCodeEnum, String msg) {
        return new DmpResult(null, statusCodeEnum.getCode(), msg);
    }

    public static <Resp> DmpResult status(boolean status) {
        return status ? success() : failed();
    }

    @JsonIgnore
    public Boolean isSuccess() {
        return StatusCodeEnum.SUC.getCode().equals(this.retCode);
    }

    @JsonIgnore
    public Boolean isData() {
        return isSuccess() && this.getRetData() != null;
    }

    public Resp getRetData() {
        return retData;
    }

    public void setRetData(Resp retData) {
        this.retData = retData;
    }

    public String getRetCode() {
        return retCode;
    }

    public void setRetCode(String retCode) {
        this.retCode = retCode;
    }

    public String getRetMsg() {
        return retMsg;
    }

    public void setRetMsg(String retMsg) {
        this.retMsg = retMsg;
    }

}
